﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dal;
using Policlinica;
using System.Data;
using System.Data.SqlClient;
using NLog;

namespace ADONetDal
{
    public class ADONetRepositoryCabinet : ADONetRepositoryBase<Cabinet>
    {
        private static Logger logger = LogManager.GetCurrentClassLogger();

        protected override SqlCommand GetInsertCommand(Cabinet entity)
        {
            var cmd = new SqlCommand("CabinetInsert", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("CabinetNumber", entity.CabinetNumber));
            logger.Trace("SqlCommand для записи данных Кабинета в базу сформирован");
            return cmd;
        }

        protected override SqlCommand GetDeleteCommand(int id)
        {
            var cmd = new SqlCommand("CabinetDelete", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("Id", id));
            logger.Trace("SqlCommand для логического удаления Кабинета id = {0} сформирован", id);
            return cmd;
        }

        protected override SqlCommand GetUpdateCommand(Cabinet entity)
        {
            var cmd = new SqlCommand("CabinetUpdate", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("Id", entity.Id));
            cmd.Parameters.Add(new SqlParameter("CabinetNumber", entity.CabinetNumber));
            logger.Trace("SqlCommand для обновления данных Кабинета id = {0} в базе сформирован", entity.Id);
            return cmd;
        }

        protected override SqlCommand GetSelectCommand(int id)
        {
            var cmd = new SqlCommand("GetCabinet", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("Id", id));
            logger.Trace("SqlCommand для поиска Кабинета id = {0} сформирован", id);
            return cmd;
        }

        protected override SqlCommand GetSelectAllCommand()
        {
            throw new NotImplementedException();
        }

        protected override SqlCommand GetSelectByParameterCommand(object parameter, string columnName)
        {
            var cmd = new SqlCommand("GetCabinetByParameter", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("Parameter", parameter));
            cmd.Parameters.Add(new SqlParameter("Column", columnName));
            logger.Trace("SqlCommand для поиска Кабинета {0} = {1} сформирован", columnName, parameter);
            return cmd;
        }

        protected override Cabinet GetEntity(SqlDataReader sqlDataReader)
        {
            Cabinet cabinet = new Cabinet();
            cabinet.Id = (int)sqlDataReader["Id"];
            cabinet.CabinetNumber = sqlDataReader["CabinetNumber"].ToString();
            return cabinet;
        }
    }
}
